/**
 * Created by Administrator on 2017/4/7.
 */
"use strict";
swan_pc
    .controller("PersonalCompanyController",function($scope,CompanyInfo,$rootScope,ngDialog,$timeout){
        $scope.displayMode = 1;
        $scope.changeDisplayMode = function(option){
            $scope.displayMode = option;
            if($scope.displayMode==3){
                $scope.initAddCompanyInfo();
            }
        };
        $scope.companyList=[];
        $scope.recursionCompanyList = [];
        $scope.loadCompany = function(){
            CompanyInfo.queryList(function(result){
                $scope.companyList = result;
                //console.log(result);
            });
            CompanyInfo.queryAll(function(result){
                $scope.recursionCompanyList = result;
                // console.log(result);
            })
        };
        $scope.loadCompany();

        $scope.click=function(event){
            console.log("click");
        };
        $scope.change = function(event){
            console.log("change");
        };
        $scope.returnDate = function(item){
            return new Date(item);
        };








        //编辑公司信息
        $scope.initAddCompanyInfo = function(){
            $scope.addCompanyInfo={
                "addresss": [
                    // {
                    //     "area": null,
                    //     "city": null,
                    //     "detailAddress": null,
                    //     "district": null,
                    //     "house": null,
                    //     "postcode": null,
                    //     "province": null,
                    //     "street": null
                    // }
                ],
                "businessConnectionWithMe": null,
                "companyBaseInfo": {
                    "alias": null,
                    "businessConnection": null,
                    "englishName": null,
                    "foundDate": null,
                    "friendship": null,
                    "importance": null,
                    "isRelated": false,
                    "isTop": true,
                    "name": null,
                    "overview": null,
                    "registerCapital": null,
                    "type": null,
                    "unit": null,
                    "webSite": null
                },
                "parentCompanyId":null,
                "friendshipWithMe": null,
                "importanceToMe": null,
                "lineNumbers": [
                    // {
                    //     "lineName": null,
                    //     "lineNumber": null
                    // }
                ],
                parentCompanyName:null,
                userId:$rootScope.presentAccountInfo.id
            };
        };
        $scope.initAddCompanyInfo();

        $scope.editCompany = function(item){
            console.log(item);
            $scope.addCompanyInfo = item;
            $scope.addCompanyInfo.companyBaseInfo.foundDate = new Date($scope.addCompanyInfo.companyBaseInfo.foundDate);
            $scope.changeDisplayMode(4);
        };
        $scope.addLineNumber = function(){
            if($scope.addCompanyInfo.lineNumbers==null){
                $scope.addCompanyInfo.lineNumbers = [];
            }
            $scope.lineNumber = {
                lineName:null,
                lineNumber:null
            };
            $scope.addCompanyInfo.lineNumbers.push($scope.lineNumber);
        };
        $scope.deleteLineNumber = function(option){
            $scope.addCompanyInfo.lineNumbers.splice(option,1);
        };
        $scope.addAddress = function(){
            if($scope.addCompanyInfo.addresss == null){
                $scope.addCompanyInfo.addresss = [];
            }
            $scope.address = {
                "area": null,
                "city": null,
                "detailAddress": null,
                "district": null,
                "house": null,
                "postcode": null,
                "province": null,
                "street": null
            };
            $scope.addCompanyInfo.addresss.push($scope.address);
        };
        $scope.deleteAddress = function(option){
            $scope.addCompanyInfo.addresss.splice(option,1);
        };
        $scope.saveCompanyInfo = function(){
            $scope.addCompanyInfo.companyBaseInfo.foundDate = new Date($scope.addCompanyInfo.companyBaseInfo.foundDate);
            console.log($scope.addCompanyInfo);
            if($scope.displayMode==3){
                CompanyInfo.save($scope.addCompanyInfo,function(result){
                    $scope.loadCompany();
                    $scope.changeDisplayMode(1);
                })
            }else if($scope.displayMode==4){
                CompanyInfo.update($scope.addCompanyInfo,function(result){
                    $scope.loadCompany();
                    $scope.changeDisplayMode(1);
                })
            }

        };
        $scope.selectParentCompany = function(){
            ngDialog.open({
                templateUrl: "scripts/components/personalBusiness/company/selectCompany.html",
                overlay: false,
                closeByDocument: false,
                scope: $scope ,
                controller: ["$scope", function(){
                    CompanyInfo.queryList(function(result){
                        $scope.companyList1 = result;
                    });
                    $scope.choseCompany = function(id,name){
                        $scope.choseParentCompany(id,name);
                        ngDialog.close();
                    }
                }]
            })
        };
        $scope.choseParentCompany = function(id,name){
            $scope.addCompanyInfo.parentCompanyId = id;
            $scope.addCompanyInfo.parentCompanyName = name;
        };

        $scope.businessConnectionList = [
            {
                name:"客户",
                value:"0"
            },
            {
                name:"合作伙伴",
                value:"1"
            },
            {
                name:"竞争对手",
                value:"2"
            },
            {
                name:"供应商",
                value:"3"
            },
            {
                name:"其他",
                value:"4"
            }
        ];
        $scope.importanceList = [
            {
                name:"不需要",
                value:"0"
            },
            {
                name:"需要",
                value:"1"
            },
            {
                name:"重要",
                value:"2"
            },
            {
                name:"很重要",
                value:"3"
            },
            {
                name:"极其重要",
                value:"4"
            }
        ];
        $scope.friendShipList = [
            {
                name:"讨厌",
                value:"0"
            },
            {
                name:"烦",
                value:"1"
            },
            {
                name:"认识",
                value:"2"
            },
            {
                name:"接受",
                value:"3"
            },
            {
                name:"好感",
                value:"4"
            }
        ];


        $scope.deleteCompanys = function(){
            console.log($scope.selected);
            _.forEach($scope.selected,function(data){
                CompanyInfo.delete({infoid:data},function(result){})
                $timeout(function(){
                    $scope.loadCompany();
                },500);
            })


        };









        //排序
        $scope.orderByKey=function(key,eve){
            $scope.reverse = ($scope.orderKey === key) ? !$scope.reverse : false;
            $scope.orderKey = key;
        };
        $scope.orderList=function(item){
            return item[$scope.orderKey];
        };
        //checkbox实现相关
        //创建变量用来保存选中结果
        $scope.selected = [];
        var updateSelected = function (action, id) {
            if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
            if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
        };
        //更新某一列数据的选择
        $scope.updateSelection = function ($event, id) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            updateSelected(action, id);
        };
        //全选操作
        $scope.selectAll = function ($event) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            for (var i = 0; i < $scope.companyList.length; i++) {
                var toDo = $scope.companyList[i];
                updateSelected(action, toDo.id);
            }
        };
        $scope.isSelected = function (id) {
            return $scope.selected.indexOf(id) >= 0;
        };
        $scope.isSelectedAll = function () {
            return $scope.selected.length === $scope.companyList.length;
        };






    });